package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvSaSaleOrder;
import com.cn.transfer.common.entity.VvSaSaleOrderB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.PurchaseOrder;
import com.cn.transfer.common.vo.PurchaseOrderDetail;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvSaSaleOrderBMapper;
import com.cn.transfer.mapper.VvSaSaleOrderMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/9/14 14:06
 * @Description: 定时任务更新销售订单
 */
@Component
public class SaleOrderUpdateXxlJob{
    @Autowired
    private VvSaSaleOrderBMapper vvSaSaleOrderBMapper;
    @Autowired
    private VvSaSaleOrderMapper vvSaSaleOrderMapper;


    @Transactional
    @XxlJob("SaleOrderUpdate")
    public void saleOrderUpdate(){
        List<VvSaSaleOrder> vvSaSaleOrders =vvSaSaleOrderMapper.byChangedateList();
        int i = 0;
        for (VvSaSaleOrder vvSaSaleOrder : vvSaSaleOrders) {
            if (vvSaSaleOrderMapper.inquireIsExist(vvSaSaleOrder.getDocumentnumber())==0){
                continue;
            }
            VvSaSaleOrder vvSaSaleOrderBd = vvSaSaleOrderMapper.byOrderDocumentnumber(Integer.valueOf(vvSaSaleOrder.getSalesorderid()));   // 表单
            boolean b = ConnmeUtils.deepEquals(vvSaSaleOrder, vvSaSaleOrderBd);
            if (!b) {
                if (vvSaSaleOrderMapper.inquireIsExist(vvSaSaleOrder.getDocumentnumber())>0){
                    // 不一致同步到视图和简道云
                    vvSaSaleOrderMapper.deleteSaleOrderId(vvSaSaleOrder.getSalesorderid());
                    vvSaSaleOrderBMapper.deleteSaleOrderBDTO(vvSaSaleOrder.getSalesorderid());
                }
                Header[] headers = ConnmeUtils.createHeaders();
                Map<String, Object> parameterDelete = new HashMap<>();
                parameterDelete.put("app_id", "6500165a60a971000ab0ad28");
                parameterDelete.put("entry_id", "65015e41ddd8340007a1698d");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameterDelete, headers, vvSaSaleOrder.getDocumentnumber(),"documentnumber");
                if (jdyPurchaseOrders.size() > 0) {
                    vvSaSaleOrderMapper.insertVVSaleOrder(vvSaSaleOrder);
                    for (VvSaSaleOrderB vvSaSaleOrderB : vvSaSaleOrder.getVvsasaleorderblist()) {
                        vvSaSaleOrderBMapper.insertSaleOrderDetail(vvSaSaleOrderB);
                    }

                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "6500165a60a971000ab0ad28");
                    parameterInster.put("entry_id", "65015e41ddd8340007a1698d");
                    parameterInster.put("data_id", jdyPurchaseOrders.get(0).get_id());
                    parameterInster.put("data",ConnmeUtils.convertEntityToMap(vvSaSaleOrder));
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                    ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);
                    i++;
                }
            }

        }
    }
}
